//
//  FeedbackViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2018/12/28.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "FeedbackViewController.h"
#import "FFTagsView.h"
#import "UITextView+WZB.h"
#import "MineServer.h"
@interface FeedbackViewController ()
@property (nonatomic,strong) UITextView *textView;
@property (nonatomic,strong) UITextField *userNameTF;
@property (nonatomic,strong) UITextField *phoneTF;
@property (nonatomic,assign) NSInteger type;
@property (nonatomic,strong) MineServer *mineServer;
@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI{
    FFTagsViewConfig *config = [[FFTagsViewConfig alloc] init];
    config.itemHeight = AdaptedWidth(28.0f);
    config.itemHerMargin = kNormalSpace;
    config.itemVerMargin = AdaptedWidth(20.0f);;
    config.topBottomSpace = AdaptedWidth(20.0f);
    config.itemContentEdgs = AdaptedWidth(11);
    config.isCanSelected = YES;
    config.hasBorder = YES;
    config.borderRadius =  AdaptedWidth(28.0f)*.5;
    config.normalBgImage = [UIImage imageWithColor:[UIColor colorWithHexString:@"#F7F7F7"]];
    config.selectedBgImage = [UIImage imageWithColor:[UIColor colorWithHexString:@"#FFCC03"]];
    config.selectedTitleColor = UIColor.whiteColor;
    config.fontSize = 15;
    NSArray*titles = @[XLocalized(@"功能意见"),XLocalized(@"体验建议"),XLocalized(@"内容建议"),XLocalized(@"其他")];
    FFTagsView *tagsView = [[FFTagsView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1.0f) tagsArray:titles config:config];
//    UIView*tagsbackV = [UIView new];
//    [tagsbackV addSubview:tagsView];
    [self.contentView addSubview:tagsView];
    [tagsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo([tagsView heighttagsArray:titles config:config]);
    }];
    kWeakSelf(self);

    tagsView.tagBtnClickedBlock = ^(FFTagsView *aTagsView, UIButton *sender, NSInteger tag) {
        weakself.type = tag+1;
    };
    
    UILabel *tipLab1 = [UILabel new];
    tipLab1.textColor = UIColorHex(#333333);
    tipLab1.font = kBoldFont(16);
    tipLab1.text = XLocalized(@"反馈内容");
    [self.contentView addSubview:tipLab1];
    [tipLab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.top.equalTo(tagsView.mas_bottom);
    }];
    self.textView = [UITextView new];
    self.textView.backgroundColor = UIColorHex(#F7F7F7);
    self.textView.font = kDefaultFont(13);
    self.textView.layer.cornerRadius = AdaptedWidth(10);
    self.textView.wzb_placeholder = XLocalized(@"请输入您的反馈内容");
    [self.contentView addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tipLab1.mas_bottom).offset(AdaptedWidth(15));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(100));
    }];
    
    UILabel *tipLab2 = [UILabel new];
    tipLab2.textColor = UIColorHex(#333333);
    tipLab2.font = kBoldFont(16);
    tipLab2.text = XLocalized(@"联系人（必填）");
    [self.contentView addSubview:tipLab2];
    [tipLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.top.equalTo(self.textView.mas_bottom).offset(AdaptedWidth(20));
    }];
    self.userNameTF = [UITextField new];
    self.userNameTF.backgroundColor = UIColorHex(#F7F7F7);
    self.userNameTF.font = kDefaultFont(13);
    self.userNameTF.layer.cornerRadius = AdaptedWidth(10);
    self.userNameTF.placeholder = XLocalized(@"请输入您的姓名");
    UIView*leftV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    self.userNameTF.leftView= leftV;
    self.userNameTF.leftViewMode = UITextFieldViewModeAlways;
    [self.contentView addSubview:self.userNameTF];
    [self.userNameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tipLab2.mas_bottom).offset(AdaptedWidth(15));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(37));
    }];
    UILabel *tipLab3 = [UILabel new];
     tipLab3.textColor = UIColorHex(#333333);
    tipLab3.font = kBoldFont(16);
    tipLab3.text = XLocalized(@"联系方式(必填)");
    [self.contentView addSubview:tipLab3];
    [tipLab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.top.equalTo(self.userNameTF.mas_bottom).offset(AdaptedWidth(20));
    }];
    self.phoneTF = [UITextField new];
    self.phoneTF.backgroundColor = UIColorHex(#F7F7F7);
    self.phoneTF.font = kDefaultFont(13);
    self.phoneTF.layer.cornerRadius = AdaptedWidth(10);
    self.phoneTF.placeholder = XLocalized(@"请输入您的手机号码");
    UIView*leftV1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    self.phoneTF.leftView= leftV1;
    self.phoneTF.leftViewMode = UITextFieldViewModeAlways;
    [self.contentView addSubview:self.phoneTF];
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tipLab3.mas_bottom).offset(AdaptedWidth(15));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(37));
    }];
    
    XThemeFlatButton *bottomBtn = [XThemeFlatButton button];
    [bottomBtn setTitle:XLocalized(@"提交") forState:UIControlStateNormal];
    [self.contentView addSubview:bottomBtn];
    [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.top.equalTo(self.phoneTF.mas_bottom).offset(AdaptedWidth(50));
        make.height.mas_equalTo(AdaptedWidth(40));
        make.bottom.mas_equalTo(-AdaptedWidth(40));
    }];
    
    [bottomBtn addTapBlock:^(UIButton * _Nonnull btn) {
        if (!weakself.type) {
            [weakself.view makeToast:XLocalized(@"请选择反馈类型")];
            return;
        }
        if (!weakself.textView.text.length) {
            [weakself.view makeToast:weakself.textView.wzb_placeholder];
            return;
        }
        if (!weakself.userNameTF.text.length) {
            [weakself.view makeToast:weakself.userNameTF.placeholder];
            return;
        }
        if (!weakself.phoneTF.text.length) {
            [weakself.view makeToast:weakself.phoneTF.placeholder];
            return;
        }
        weakself.mineServer.feedbackType = weakself.type;
        weakself.mineServer.content = weakself.textView.text;
        weakself.mineServer.contact = weakself.userNameTF.text;
        weakself.mineServer.contact_way = weakself.phoneTF.text;
        [MBProgressHUD showLoadTitile:@"" ToView:weakself.view];
        [weakself.mineServer feedbackComplete:^(BOOL success, NSString * _Nonnull msg) {
            [MBProgressHUD hideHUDForView:weakself.view];
            [kAppWindow makeToast:msg];
            if (success) {
                [weakself backBtnClicked];
            }
        }];
    }];
}
- (MineServer*)mineServer{
    if (!_mineServer) {
        _mineServer = [MineServer new];
    }
    return _mineServer;
}
@end
